//
//  RegisterViewController.m
//  WisdomZhuHai
//
//  Created by yewei on 14/10/30.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "RegisterViewController.h"
#import "Common.h"
#import "LoginViewController.h"
#import "TransforController.h"
#import "ProtocolManager.h"

@interface RegisterViewController ()

@property (nonatomic,strong) IBOutlet UITextField *accountTextField;
@property (nonatomic,strong) IBOutlet UITextField *passwordTextField;
@property (nonatomic,strong) IBOutlet UIView *inputBackView;
@property (nonatomic,strong) IBOutlet UIImageView *lineView1;
@property (nonatomic,strong) IBOutlet UIImageView *lineView2;
@property (nonatomic,strong) IBOutlet UIButton *registerButton;
@property (nonatomic,strong) IBOutlet UIButton *protocolButton;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sidebar/background.png"]];
//    self.inputBackView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"center/Bottom.png"]];
//    self.lineView1.image = [UIImage imageNamed:@"center/Division.png"];
//    self.lineView2.image = [UIImage imageNamed:@"center/Division.png"];
//    self.accountTextField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"center/Avatars1.png"]];
//    self.accountTextField.leftViewMode = UITextFieldViewModeAlways;
//    self.passwordTextField.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"center/Cipher1.png"]];
//    self.passwordTextField.leftViewMode = UITextFieldViewModeAlways;
//    [self.protocolButton setTitleColor:[Common colorWithRGB:0xf8b62d] forState:UIControlStateNormal];
//    [self.registerButton setBackgroundImage:[UIImage imageNamed:@"center/Options1.png"] forState:UIControlStateNormal];
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewTapped:(UITapGestureRecognizer*)tapGr{
    NSLog(@"viewTapped");
    [self.accountTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}

+(void)registerWithTarget:(id)target selector:(SEL)sel account:(NSString *)account passWord:(NSString *)passWord
{
    //手机型号
    NSString* phoneModel = [[UIDevice currentDevice] model];
    //物理地址
    //NSString* macAddr = [Common getMacAddr];
    NSString *phoneUUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    [[ProtocolManager shareInstance] register:account password:passWord mobileModel:phoneModel loginCity:@"" mac:phoneUUID target:target selector:sel];
}

-(IBAction)registerClick:(id)sender
{
    //注册
    NSString *account = [self.accountTextField text];
    NSString *passWord = [self.passwordTextField text];
    NSString *alert = @"";
    if([account length] == 0){
        alert = @"账户名不能为空！";
    }else if([passWord length] == 0){
        alert = @"密码不能为空！";
    }else if([account length] > 20){
        alert = @"用户名过长！";
    }else if([account length] < 4){
        alert = @"用户名过短！";
    }else if([passWord length] > 18){
        alert = @"密码过长！";
    }else if([passWord length] < 6){
        alert = @"密码过短！";
    }
    if([alert length] != 0)
    {
        [[Common shareInstance] showAlertViewWithTitle:nil text:alert dismissTime:1.0];
        return;
    }
    [[Common shareInstance] showLoadingViewWithMessage:nil];
    [RegisterViewController registerWithTarget:self selector:@selector(didFinishRigister:) account:account passWord:passWord];
}

-(void)didFinishRigister:(NSDictionary *)dic
{
    [[Common shareInstance] stopLoadingView];
    if(dic == nil)
    {
        NSLog(@"RegisterViewController didFinishRigister dic is nil");
    }
    NSString *respCode = [Common valueForKey:P_RESPCODE dic:dic];
//    NSString *respDes = [Common valueForKey:P_RESPDESC dic:dic];
//    [[Common shareInstance] showAlertViewWithTitle:nil text:respDes dismissTime:100.0];
    if(![respCode isEqualToString:CODE_HANDLE_SUCCESS])
    {
        return;
    }
    //发送注册成功的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFIY_REGISTER_SUCCESS object:nil userInfo:dic];
    //返回登陆界面
    [TransforController umengEvent:@"UserRegisterSuccess" attributes:@{@"loginname" : [self.accountTextField text]} number:@(1) ct:@"900002"];
    [[Common shareInstance] showAlertViewWithTitle:nil text:@"注册成功" dismissTime:1.0];
    [self.loginDelegate setAccount:self.accountTextField.text];
    [self.navigationController performSelector:@selector(popViewControllerAnimated:) withObject:[NSNumber numberWithBool:YES] afterDelay:0.8];
//    [LoginViewController loginWithTarget:self selector:@selector(didFinishAutoLogin:) account:self.accountTextField.text passWord:self.passwordTextField.text];
    
    
}

-(void)didFinishAutoLogin:(NSDictionary *)dic
{
    [[Common shareInstance] stopLoadingView];
    if(dic == nil)
    {
        NSLog(@"RegisterViewController didFinishAutoLogin dic is nil");
        return;
    }
    NSString *respCode = [Common valueForKey:P_RESPCODE dic:dic];
    if([respCode isEqualToString:CODE_HANDLE_SUCCESS])
    {
        [ProtocolManager shareInstance].sid = [Common valueForKey:P_SESSIONID dic:dic];
        [ProtocolManager shareInstance].loginName = self.accountTextField.text;
        //发送登陆成功的通知
        
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFIY_LOGIN_SUCCESS object:nil userInfo:dic];
    }

}

-(IBAction)protocolClick:(id)sender
{
    //使用条款
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
